<script lang="ts" setup>
import type { FieldValues, PlusColumn } from 'plus-pro-components'

import { memberAddTagList, memberCreateTagList, memberUpdateTagList } from '@fl/api/anjutong/member'
import { computed, ref } from 'vue'

// const props = withDefaults(defineProps<{
//     getTagInformation?: any
// }>(), {
//     getTagInformation: () => {
//         return {
//             conditions: [],
//         }
//     })
const props = withDefaults(defineProps<{ getTagInformation?: any }>(), {
    getTagInformation: () => {
        return {
            conditions: [],
        }
    },
})
const emits = defineEmits(['closeDialogTag'])
const { getTagInformation } = toRefs(props)

const TagCatList = ref<any>([])
// const state = ref<FieldValues>({
//     conditions: [],
//     info: '',
//     mainCat: '',
//     name: '',
//     status: 1,
//     subCat: '',
//     type: '',
// })
const addState = ref<any>({
    conditions: [],
    info: '',
    mainCat: '',
    name: '',
    status: 1,
    subCat: '',
    type: 0,
})
const newDataTag = ref<any>({})
function init() {
    if (getTagInformation.value.name)
        newDataTag.value = getTagInformation.value

    else
        newDataTag.value = addState.value

    console.log(newDataTag.value, 'newDataTag.value ')
}
init()
// const newDataTag: any = computed(() => {
//     let newnewDataTagTemp = {}
//     if (getTagInformation.value.name)
//         newnewDataTagTemp = getTagInformation.value

//     else

//         newnewDataTagTemp = addState.value

//     console.log(newnewDataTagTemp, getTagInformation.value, 'newnewDataTagTemp')
//     return newnewDataTagTemp
// })
const columns: PlusColumn[]
    = [
        {
            fieldProps: {
                placeholder: '请输入',
            },
            label: '标签名称',
            prop: 'name',
            valueType: 'copy',
            width: 120,
        },
        {
            label: '一级分类',
            options: async () => {
                const data = await memberCreateTagList()
                TagCatList.value = data ?? []
                return data.map((item: any) => {
                    return {
                        label: item.label,
                        value: item.label,
                    }
                })
            },

            prop: 'mainCat',
            valueType: 'select',
            width: 120,
        },
        {
            hideInForm: computed(() => newDataTag.value.mainCat === ''),
            label: '二级分类',
            options: computed(() => {
                if (newDataTag.value.mainCat) {
                    console.log(newDataTag.value.mainCat, '计算属性')
                    const params: any = TagCatList.value.find((item: any) => item.label === newDataTag.value.mainCat)
                    console.log(params, 'paramsparamsparams')
                    return params?.children.map((item: any) => {
                        return {
                            label: item.label,
                            value: item.label,
                        }
                    })
                }
                else {
                    return {}
                }
            }),
            prop: 'subCat',
            valueType: 'select',
            width: 120,
        },
        {
            label: '标签类型',
            options: [
                {
                    label: '手动标签',
                    value: 1,
                },
                {
                    label: '自动标签',
                    value: 0,
                },
            ],
            prop: 'type',
            valueType: 'radio',
        },
        {
            hideInForm: computed(() => newDataTag.value.type === 1),
            label: '条件设置',
            options: [
                {
                    label: '租房合同签订',
                    value: 'isGood',
                },
                {
                    label: '好房合同签订',
                    value: 'isRent',
                },
            ],
            prop: 'conditions',
            valueType: 'checkbox',
        },
        {
            fieldProps: {
                placeholder: '请输入',
            },
            hideInForm: computed(() => newDataTag.value.type === 0),
            label: '标签说明',
            prop: 'info',
            valueType: 'copy',
            width: 120,
        },
    ]

function handleChange(values: FieldValues, prop: PlusColumn) {
    console.log(values, prop, 'change')
}
function handleSubmit(values: any) {
    if (getTagInformation.value.name) {
        memberUpdateTagList(newDataTag.value).then((res) => {
            console.log(res, 'resresresresmemberAddTagList')
            if (!res) { return false }

            else {
                ElMessage({
                    message: '编辑成功',
                    type: 'success',
                })
                emits('closeDialogTag')
            }
        })
    }
    else {
        memberAddTagList(newDataTag.value).then((res) => {
            console.log(res, 'resresresresmemberAddTagList')
            if (!res) { return false }

            else {
                emits('closeDialogTag')
                handleReset()
                ElMessage({
                    message: '新增成功',
                    type: 'success',
                })
            }
        })
    }
    console.log(values, 'Submit')
}
function handleSubmitError(err: any) {
    console.log(err, 'err')
}
function handleReset() {
    emits('closeDialogTag')
    addState.value.conditions = []
    addState.value.info = ''
    addState.value.mainCat = ''
    addState.value.name = ''
    addState.value.status = 1
    addState.value.subCat = ''
    addState.value.type = 0
    console.log(newDataTag.value, 'handleReset')
}
</script>

<template>
    <el-card>
        <PlusForm v-model="newDataTag"
                  :columns="columns"
                  label-width="120"
                  submit-text="提交"
                  reset-text="返回"
                  @change="handleChange"
                  @submit="handleSubmit"
                  @submit-error="handleSubmitError"
                  @reset="handleReset"
        />
    </el-card>
</template>
